function h = kplot(plottype, varargin)
%
% plottype:	<string>
%	'pr'	precision, recall
%	'roc'	true positive, false positive
%
% varargin is one or more S, where
% S:		<structure of vectors>
%	label
%	tp
%	fp
%	fn
%	tn
%	precision
%	recall
%	f1
%
% returns:
%	h	<plot handle>
%

	nlines = size(varargin,2);

	figure;
	box on;
	axis([0,1,0,1]);
	hold all;

	linestyles = {'-','--',':','-.'};  % this is all of them
	markerstyles = {'x','d','o','s','^','*','+'};  % there are lots more markers (see matlab help)

	legendlabels = {''};

	switch(lower(plottype))

		case 'pr'
			xlabel('Recall');
			ylabel('Precision');

			for i = 1:nlines

				S = varargin{i};
				legendlabels{i} = S.label;
				
				h = plot(S.recall,S.precision);
				set(h,'LineWidth',1,{'LineStyle'}, {linestyles{i}}, {'Marker'}, {markerstyles{i}});
%				set(h,'LineWidth',1,{'LineStyle'}, {linestyles{i}});

				% label points with thresholds (only useful if small number of points)
				%text(S.recall,S.precision,num2str(S.threshold(:)));
			end
			
		case 'roc'
			xlabel('False Positive Rate');
			ylabel('True Positive Rate');

			% draw y=x line of 'no-discrimination'
			line([0,1],[0,1], 'LineWidth',1, {'LineStyle'}, {':'}, 'Color',[0.5,0.5,0.5]);

			for i = 1:nlines

				S = varargin{i};
				legendlabels{i} = S.label;

%				fpr = S.fp ./ (S.fp + S.tn);
%				tpr = S.tp ./ (S.tp + S.fn);
				n = S.fp(1) + S.tn(1);
				p = S.tp(1) + S.fn(1);
				fpr = S.fp / n;
				tpr = S.tp / p;

				h = plot(fpr,tpr);
				set(h,'LineWidth',1,{'LineStyle'}, {linestyles{i}}, {'Marker'}, {markerstyles{i}});

				% label points with thresholds (only useful if small number of points)
				%text(fpr,tpr,num2str(S.threshold(:)));
			end			
	end %switch
	
	legend(legendlabels);
end
